package mya.mes.mdc.model.tag.util;

import mya.mes.mdc.model.tag.MdcConstants;
import mya.mes.mdc.model.tag.MdcTag;
import mya.mes.mdc.model.tag.MdcTagValue;
import mya.mes.mdc.model.tag.ViewTagHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TagHolder {
  private static final Logger logger = LoggerFactory.getLogger(TagHolder.class);
  private static final TagHolder _instance = new TagHolder();

  private static final String AI = "AI";
  private static final String DI = "DI";
  private static final String ST = "ST";
  /**
   * AI 数值型
   */
  private MdcTag[] AIArray = new MdcTag[MdcConstants.MDC_TAG_MAX];
  /**
   * DI 布尔量
   */
  private MdcTag[] DIArray = new MdcTag[MdcConstants.MDC_TAG_MAX];
  /**
   * 字符串
   */
  private MdcTag[] STArray = new MdcTag[MdcConstants.MDC_TAG_MAX];

  public static TagHolder getInstance() {
    return _instance;
  }


  public MdcTag getMdcTag(MdcTagValue tagValue) {
    return getMdcTag(tagValue.tagPrefix, tagValue.tagIndex);
  }

  public MdcTag getMdcTag(String tagPrefix, int tagIndex) {
    if (tagIndex < 0) {
      throw new RuntimeException("tagIndex < 0: tagIndex = " + tagIndex);
    }

    if (tagIndex >= MdcConstants.MDC_TAG_MAX) {
      throw new RuntimeException("tagIndex > MAX_VALUE: tagIndex = " + tagIndex);
    }

    if (AI.equals(tagPrefix)) {
      return AIArray[tagIndex];
    }

    if (DI.equals(tagPrefix)) {
      return DIArray[tagIndex];
    }

    if (ST.equals(tagPrefix)) {
      return STArray[tagIndex];
    }
    throw new RuntimeException("Unknown TagPrefix:" + tagPrefix);

  }

  public void init(List<MdcTag> mdcTagList) {
    Arrays.fill(AIArray, null);
    Arrays.fill(DIArray, null);
    Arrays.fill(STArray, null);
    update(mdcTagList);
    ViewTagHolder.getInstance().build(this);
  }


  public void update(List<MdcTag> mdcTagList) {
    if (mdcTagList == null) {
      return;
    }
    mdcTagList.stream().forEach(mdcTag -> {
      if (mdcTag == null) {
        return;
      }
      if (mdcTag.tagIndex < 0 || mdcTag.tagIndex >= MdcConstants.MDC_TAG_MAX) {
        throw new RuntimeException("超出范围的tagIndex[0," + MdcConstants.MDC_TAG_MAX + "]:" + mdcTag.tagIndex);
      }
      if (AI.equals(mdcTag.tagPrefix)) {
        AIArray[mdcTag.tagIndex] = mdcTag;
        if("AI1007".equals(mdcTag.getTagName())){
          System.out.println(mdcTag.getTagName());
        }
        return;
      }
      if (DI.equals(mdcTag.tagPrefix)) {
        DIArray[mdcTag.tagIndex] = mdcTag;
        return;
      }
      if (ST.equals(mdcTag.tagPrefix)) {
        STArray[mdcTag.tagIndex] = mdcTag;
        return;
      }
      throw new RuntimeException("Unknown TagPrefix:" + mdcTag.tagPrefix);
    });
    System.out.println("AIArray:"+AIArray.length);
    System.out.println("DIArray:"+DIArray.length);
    System.out.println("STArray:"+STArray.length);
    logger.info("TagHolder 更新完成！");
  }

  public synchronized List<MdcTag> asList() {
    return asList(null);
  }

  /**
   * 如果tagPrefix == null,返回全部
   *
   * @param tagPrefix
   * @return
   */
  public synchronized List<MdcTag> asList(String tagPrefix) {
    if (AI.equals(tagPrefix)) {
      return Arrays.asList(AIArray);
    }
    if (DI.equals(tagPrefix)) {
      return Arrays.asList(DIArray);
    }
    if (ST.equals(tagPrefix)) {
      return Arrays.asList(STArray);
    }
    if (tagPrefix == null) {
      List<MdcTag> list = new ArrayList<>(AIArray.length + DIArray.length + STArray.length);
      list.addAll(Arrays.asList(AIArray));
      list.addAll(Arrays.asList(DIArray));
      list.addAll(Arrays.asList(STArray));
      return list;
    }
    throw new RuntimeException("Unknown TagPrefix:" + tagPrefix);
  }

//  private void debugInfo() {
//    for (MdcTag mdcTag : AIArray) {
//      if (mdcTag != null && mdcTag.alarmTag != null) {
//        System.out.println(mdcTag.alarmTag.toJson().encodePrettily());
//      }
//    }
//    for (MdcTag mdcTag : DIArray) {
//      if (mdcTag != null && mdcTag.alarmTag != null) {
//        System.out.println(mdcTag.alarmTag.toJson().encodePrettily());
//      }
//    }
//
//    for (MdcTag mdcTag : STArray) {
//      if (mdcTag != null && mdcTag.alarmTag != null) {
//        System.out.println(mdcTag.alarmTag.toJson().encodePrettily());
//      }
//    }
//  }
}
